package com.example.lirisheng.springbootfinalexperiment.controller;

import com.example.lirisheng.springbootfinalexperiment.entity.PersonInformation;
import com.example.lirisheng.springbootfinalexperiment.entity.Users;
import com.example.lirisheng.springbootfinalexperiment.entity1.PerInfoEntity;
import com.example.lirisheng.springbootfinalexperiment.entity1.ResAuthoritiesEntity;
import com.example.lirisheng.springbootfinalexperiment.entity1.ResidentEntity;
import com.example.lirisheng.springbootfinalexperiment.service.ResService;
import com.example.lirisheng.springbootfinalexperiment.util.QRJsonUtil;
import com.example.lirisheng.springbootfinalexperiment.util.RecodeUtil;
import com.example.lirisheng.springbootfinalexperiment.pojo.RegistryEntity;
import com.example.lirisheng.springbootfinalexperiment.util.RespBean;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashMap;
import java.util.Map;

@RestController
@RequestMapping("/weiRes")
public class WeiResController {

    @Autowired
    ResService resService;
    protected  final Logger logger = LoggerFactory.getLogger(WeiResController.class);

    @ApiOperation(value = "更改住户个人信息", notes = "更改住户个人信息")
    @PostMapping("/changeUserInfo")
    public RespBean changeUserInfo(RegistryEntity registryEntity){
        if(logger.isDebugEnabled()){
            logger.debug("entry changeInfo");
            logger.debug("registryEntity"+registryEntity);
        }

        //合法,则修改信息
         PersonInformation personInformation =new PersonInformation();
        personInformation.setName(registryEntity.getName());
        personInformation.setIdentity(registryEntity.getIdentity());
        personInformation.setHouseId(registryEntity.getHouseId());
        try{
            resService.changeInfo(personInformation);
            return   RespBean.ok("住户个人信息更改成功");
        }catch (Exception exception){
            System.out.println("messsage:"+exception.getMessage());
            return RespBean.error("更改失败");
        }
    }

    /**
     * 生成微信图片二维码
     *
     * @param request
     * @param response
     * @param content   为前端传过来的二维码的内容，即路径链接
     * @throws Exception
     */
//    @Log("微信图片二维码")
    @ApiOperation(value = "为住户生成个人信息二维码", notes = "为住户生成个人信息二维码")
    @GetMapping("/QR")
    public void qrcode(@RequestParam("openId") String openId,HttpServletRequest request, HttpServletResponse response) throws Exception {

        // 调用工具类，生成二维码
        if (logger.isDebugEnabled()){
            logger.debug("entry QR");
        }
        //通过openId获取个人信息
        PersonInformation personInformation=resService.findInfo(openId);
        QRJsonUtil qrJsonUtil = QRJsonUtil.creAndInitQR(openId,personInformation);
        //180为图片高度和宽度
        RecodeUtil.creatRrCode(qrJsonUtil.toString(), 180,180,response);
    }
}
